package com.ibeetl.cms.web;

import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthAlipayRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class RestAuthController {

    @RequestMapping("/render")
    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }

    @RequestMapping("/callback")
    public Object login(AuthCallback callback) {
        AuthRequest authRequest = getAuthRequest();
        return authRequest.login(callback);
    }

    private AuthRequest getAuthRequest() {
        return new AuthAlipayRequest(AuthConfig.builder()
                .clientId("2021001149644914")
                .clientSecret("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCeMOSc4cMfvupzT61PUKRmDyKdeLTngeEBWCOMJ5kDZiVewTyvXYbUTGJ1ZWvrcZOlKWA5FteJcd8QbAxLi6ObfBxnYJgD1T7WE46wlmAHR7uj/S7tH8NeQb2CQQqK4V560nT4lBDWSnAbUo9+KauU6hrG7xLPQWBc30CbcNJ5fdo1nDaSKqaANXR3RBdrx2qPIPle7lX/OYrYLgmtE1JZr3XnQpCcmZRUleGDZyJdCDdsXZu4BABM4+hNKyY+Cox/Jf8s3MwL4tfQ6Y4ZnB+/B6WGWtyL3rU/DA2ExEya26HkqZVAzvAhGdVNIu0jmJGz1p/of/wfy4Pd6ChfPQO3AgMBAAECggEAC7RQZe8qUcH9dNSzXQf5cfeytvYEZJCuhjvwPWF8p8y5/R3NEKiZ3kFck5USfBQmOXUrQm4XOnsrZvqnmjtVup5fN6Lk/Hudr5c42oX1yvQaxRJpk2/jM6knjWikkAwGwo2py+6gpEpTqyHbrhkFMfp0Sr3LpXwP1i9Kh3W8jPKNguClndx7269SNuTLFGFOD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80FfqGoyiMWgsk9+emNcSunq++WHxTN5BZo/V6da0vKtarvgB6E+S//cDs9EaVcWp3UHgF8z+hvnMUjwFz0Dujwxir2PkKV1eEqMnFu/M9t0FmZsmaawE4Dzfkfz28EpHcKsswBqFg5wqmPApEXZR9e2Y8cdnccDhFBFQwTBdBIjauKa5G0rMUuJjJtwn+DN3pgnCEtWma3o9N6R2RdHDuXdOaBBoyeW0x8/lp7pQLoBRvxNQBW7MJdTihc3c58J7EgR1jdepkwMZMAsy+gz2TJBBBwYECVYWl9z9yHF34owjiY/d3ryn0C22WBsbpQRNBAU1yQDNgmiKyhKwhfS9QIDAQAB")
                .redirectUri("http://b2944625b9.zicp.vip/cms/aliLogin/returnAddress.do")
                .build());
    }
}